package com.hellowx.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by IntelliJ IDEA.
 *
 * @author MagicMushroom
 * @date 2021/7/20
 */
@RestController
@RequestMapping("/sentinel")
public class SentinelController {
    @GetMapping("/test")
    @SentinelResource(value = "test",blockHandler ="blockHandler",fallback = "fall")
    public String test(Integer id){
       return "test ok"+id;
    }

    public String blockHandler(Integer id, BlockException e){
        if (e instanceof FlowException){
            return "FlowException 错误！";
        }
        if (e instanceof DegradeException){
            return "DegradeException 错误！";
        }
        if (e instanceof ParamFlowException){
            return "ParamFlowException 错误！";
        }
        return "其他错误";
    }

    public String fall(Integer id){
        return "自定义异常";
    }
}
